(function () {
    var price = '';

    getPrice();

    pay();

    //获取价格
    function getPrice() {
        price = $('.pay-price-active').attr('data-price');

        //绑定充值面额点击事件
        $('.pay-default').on('click','.pay-default-price',function (e) {
            e = e || event;
            //get number
            e.preventDefault();
            var $target = $(e.target);
            $('.pay-input-price').val('');
            price = $target.attr('data-price');
            //changeActive
            setActiveClass( e.target, 'pay-price-active' );
            changePriceShow( price );
        });

        //绑定input事件
        $('.pay-input-price').on({
            focus: function (e) {
                e = e || event;
                //get number
                e.preventDefault();
                $('.pay-input-price').val('');
                price = 0;
                changePriceShow(0);
                setActiveClass( e.target, 'pay-price-active' );
            },
            input: function (e) {
                e = e || event;
                //get number
                e.preventDefault();
                var priceNum = $(this).val().match(/^\d+\.?\d*$/i);
                if( priceNum ){
                    price = Math.floor( priceNum[0] );
                }
                changePriceShow( price );
            }
        });
    }

    //绑定充值事件
    function pay() {
        var $payBtn = $('#paySubmit');
        if( $payBtn ){
            $payBtn
                .off('click')
                .on('click', function (e) {
                    e = e || event;
                    e.preventDefault();
                    //判断是否登录
                    var member = $('[name="member"]').val();
                    if( price >= 1 ){
                        var newTab = window.open('about:blank');
                        $.ajax({
                            type: "POST",
                            url: ' /pay/alipay_pay_url',
                            dataType:"json",
                            data:{
                                member: member,
                                num : price
                            },
                            success: function (res) {
                                if ( res.ret === 0 ) {
                                    //暂时用系统框代替
                                    newTab.location.href = res.data.url;

                                } else {
                                    alert( res.msg );
                                    newTab.close();
                                }
                            }
                        });
                    }else{
                        alert('请输入大于1的金额');
                    }
                });
        }
    }


    // 修改充值面额展示的价格
    function changePriceShow( num ) {
        $('.input-price-show span:first-child').text( num );
    }

    //changeActiveClass(传参，设置作用范围)
    function setActiveClass( target,  activeClass ) {
        var $target = target instanceof jQuery ? target :  $( target ),
            $prevActiveNodes = $('.' + activeClass ),
            len = $prevActiveNodes.length,
            i = 0;

        for (i; i < len; i++){
            $($prevActiveNodes[i]).removeClass( activeClass );
        }

        $target.addClass( activeClass);
    }

})();